#include "timer.h"
#include "led.h"
#include <stdbool.h>
#include "OpenImu.h"
#include "fault_code.h"

struct TimeFlag SysTime={0,0};
struct Time  ImuTime ={0.0,0,0,0,0,0};
struct Task  DriftTask={0.01,false,false,0};
struct UartTime Uart3Time={0,0, 0};
struct UartTime Uart4Time={0,0, 0};

//通用定时器3中断初始化
//这里时钟选择为APB1的2倍，而APB1为36M
//arr：自动重装值。
//psc：时钟预分频数
//这里使用的是定时器3!
u8 TimeXmsFlag=0;
u8 Integr_Flag_1ms=0;   //1ms中断标志
u8 CalibrationTimes=0;  //第二次校准进入中断的次数标志
u8 Integr_Flag =0;      //积分10ms中断标志
u8 Drinft_1S_flag  = 0;  //开机1s确定漂移量
u16 Drinft_1S_cnt  = 0;  //开机1s计数
int Drift_Loop_Cnt = 0;   //1S校准次数


void TIM2_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能  
  //定时器TIM3初始化
  TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值  
  TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
//  TIM_ITConfig(TIM2,TIM_IT_Update,DISABLE ); //使能指定的TIM3中断,允许更新中断
  //中断优先级NVIC设置
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  //TIM3中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级0级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;  //从优先级3级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
//  TIM_Cmd(TIM2, DISABLE);  //先去使能定时器       
}

void TIM3_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能  
  //定时器TIM3初始化
  TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值  
  TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx更新中断标志 
  //中断优先级NVIC设置
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //先占优先级0级       //0 3
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //从优先级3级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
  TIM_Cmd(TIM3, ENABLE);  //使能TIMx           
}


extern "C" {  
    
//定时器2中断服务程序 ，10ms一次中断
void TIM2_IRQHandler(void)   //TIM3中断
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
    {
     //   TIM_ClearITPendingBit(TIM2, TIM_IT_Update);  //清除TIMx更新中断标志 
        DriftTask.time_flag=true;
//        Imu_Adis16505::get()->ImuUpdateDrift();
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);  //清除TIMx更新中断标志 

    } 
}


//定时器3中断服务程序
void TIM3_IRQHandler(void)   //TIM3中断
{
    int MaxTimeOut=0;
    static int cnt=0;
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
    {
        
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx更新中断标志 
        Integr_Flag=1;
        DriftTask.time_flag=true;
        
         if(DriftTask.task_flag== true) 
         {
           DriftTask.task_sum_cnt++;                              //进入中断次数+1
         }
        cnt++;
         
//       Imu_Adis16505::get()->BurstModeAcc_32Bit(); 
        SysTime.Time1_Flag=1;                                             //刷新中断标志 
        if(cnt==100)
        {
         SysTime.Time10_falg=1;                                             //刷新中断标志 
        }
         
        ImuTime.msec= cnt;
        if(cnt==50)
        {
          ImuTime.sec++;                  //满1s加1，10ms进入一次，1s进入100
          cnt=0;  
          LED3=!LED3_Status;              //1s翻转一次
        }
        if(ImuTime.sec==60)               //秒钟加到60，达到1分钟
        {
          ImuTime.sec=0;                  //清零秒钟
          ImuTime.min++;                  //进入一次，分钟加1
        }
        if(ImuTime.min==60)               //分钟加到60，达到1小时
        {
          ImuTime.min=0;                  //分钟清零    
          ImuTime.hour++;                 //进入一次，小时加1
        }
        if(ImuTime.hour==24)              //进入一次，天加1
        {
           ImuTime.hour=0;                //小时清零
           ImuTime.day++;                 //进入一次天数加1
        }
        if(ImuTime.day>=365)              //若天数大于365天，则清零天数
        {
          ImuTime.day=0;
        }
        ImuTime.nowtime= (ImuTime.sec+ImuTime.min*60+ImuTime.hour*3600+ ImuTime.day* 86400)*1000+ImuTime.msec*1;         //得到系统当前ms数
        
        
        Uart3Time.Cnt= cnt;           //更新缓存时间
        Uart3Time.NowTime=cnt;        //更新当前时间
        
        if((Uart3Time.NowTime>=0)&&(Uart3Time.NowTime<=15)&&(Uart3Time.LastTime>=85)&&(Uart3Time.LastTime<=100))
        {
         MaxTimeOut= Uart3Time.NowTime-(100-Uart3Time.LastTime);     //计算溢出时间
        
        }
        else
        {
          MaxTimeOut= Uart3Time.NowTime-Uart3Time.LastTime;     //计算溢出时间
        }
        
        if((MaxTimeOut>10)||(MaxTimeOut<-10))
        {
          WingbotFault.UartWingbot.FaultSensor.TimeOutFlag =1;      //如果时间溢出则置为标志位
        }
        else
        {
          WingbotFault.UartWingbot.FaultSensor.TimeOutFlag =0;      //否则清零
        }
                                                                    
        SysTime.Time1_Flag=1;                                             //刷新中断标志
        
        
//        openImu->ImuAcc_32Bit_DataHandle(openImu->OpenImu_Data.Original.Burst32_Data) ;
        
//        printf("%d \r\n",0x1);
//        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx更新中断标志

    }       
}

}












